package com.pdg.android.splorr.data;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.pdg.android.splorr.R;
import com.pdg.android.splorr.data.baseclasses.DataClient;
import com.pdg.android.splorr.data.enums.ArmorType;
import com.pdg.android.splorr.data.enums.ItemType;
import com.pdg.android.splorr.data.enums.LockDetail;
import com.pdg.android.splorr.data.enums.LockMaterial;
import com.pdg.android.splorr.data.enums.WeaponType;

public abstract class Item<T> extends DataClient<T> {
	
	private static final ItemType DEFAULT_ITEM_TYPE = ItemType.NONE;
	private static final LockMaterial DEFAULT_LOCK_MATERIAL = LockMaterial.NONE;
	private static final LockDetail DEFAULT_LOCK_DETAIL = LockDetail.NONE;
	private static final String SUBKEY_ITEM_TYPE = "item_type";
	private static final String SUBKEY_LOCK_MATERIAL = "lock_material";
	private static final String SUBKEY_LOCK_DETAIL = "lock_detail";
	private static final ArmorType DEFAULT_ARMOR_TYPE = ArmorType.NONE;
	private static final int DEFAULT_DURABILITY = 0;
	private static final String SUBKEY_ARMOR_TYPE = "armor_type";
	private static final String SUBKEY_DURABILITY = "durability";
	private static final WeaponType DEFAULT_WEAPON_TYPE = WeaponType.NONE;
	private static final String SUBKEY_WEAPON_TYPE = "weapon_type";
	private ItemType type = DEFAULT_ITEM_TYPE;
	private LockMaterial lockMaterial = DEFAULT_LOCK_MATERIAL;
	private LockDetail lockDetail = DEFAULT_LOCK_DETAIL;
	private ArmorType armorType = DEFAULT_ARMOR_TYPE;
	private int durability = DEFAULT_DURABILITY;
	private WeaponType weaponType = DEFAULT_WEAPON_TYPE;

	public Item(T theData) {
		super(theData);
	}
	
	public Item(T theData, Item<?> copyFrom){
		super(theData);
		setType(copyFrom.getType());
		setLockMaterial(copyFrom.getLockMaterial());
		setLockDetail(copyFrom.getLockDetail());
		setArmorType(copyFrom.getArmorType());
		setDurability(copyFrom.getDurability());
		setWeaponType(copyFrom.getWeaponType());
	}

	@Override
	protected void onLoad(SharedPreferences thePreferences) {
		setType(ItemType.valueOf(thePreferences.getString(getSubKey(SUBKEY_ITEM_TYPE), DEFAULT_ITEM_TYPE.name())));
		setLockMaterial(LockMaterial.valueOf(thePreferences.getString(getSubKey(SUBKEY_LOCK_MATERIAL), DEFAULT_LOCK_MATERIAL.name())));
		setLockDetail(LockDetail.valueOf(thePreferences.getString(getSubKey(SUBKEY_LOCK_DETAIL), DEFAULT_LOCK_DETAIL.name())));
		setArmorType(ArmorType.valueOf(thePreferences.getString(getSubKey(SUBKEY_ARMOR_TYPE), DEFAULT_ARMOR_TYPE.name())));
		setWeaponType(WeaponType.valueOf(thePreferences.getString(getSubKey(SUBKEY_WEAPON_TYPE), DEFAULT_WEAPON_TYPE.name())));
		setDurability(thePreferences.getInt(getSubKey(SUBKEY_DURABILITY), DEFAULT_DURABILITY));
	}

	protected abstract String getSubKey(String theSubKey);

	@Override
	protected void onCommit(Editor theEditor) {
		theEditor.putString(getSubKey(SUBKEY_ITEM_TYPE), getType().name());
		theEditor.putString(getSubKey(SUBKEY_LOCK_MATERIAL), getLockMaterial().name());
		theEditor.putString(getSubKey(SUBKEY_LOCK_DETAIL), getLockDetail().name());
		theEditor.putString(getSubKey(SUBKEY_ARMOR_TYPE), getArmorType().name());
		theEditor.putString(getSubKey(SUBKEY_WEAPON_TYPE), getWeaponType().name());
		theEditor.putInt(getSubKey(SUBKEY_DURABILITY), getDurability());
	}

	public ItemType getType() {
		return type;
	}
	
	public boolean isKey() {
		return getType()==ItemType.KEY;
	}


	public void setType(ItemType type) {
		this.type = type;
	}

	public LockMaterial getLockMaterial() {
		return lockMaterial;
	}

	public void setLockMaterial(LockMaterial lockMaterial) {
		this.lockMaterial = lockMaterial;
	}

	public LockDetail getLockDetail() {
		return lockDetail;
	}
	
	public String getName(){
		switch(getType()){
		case KEY:
			return getKeyName();
		case ARMOR:
			return getArmorName();
		case WEAPON:
			return getWeaponName();
		default:
			return null;
		}
	}

	private String getArmorName() {
		return String.format(getContext().getString(R.string.room_items_armor_format),
				getContext().getString(getArmorType().getStringId()),
				getDurability(),
				getArmorType().getDurability()
				);
	}

	private String getWeaponName() {
		return String.format(getContext().getString(R.string.room_items_weapon_format),
				getContext().getString(getWeaponType().getStringId()),
				getDurability(),
				getWeaponType().getDurability()
				);
	}

	private String getKeyName() {
		return String.format(getContext().getString(R.string.room_items_key_format),
				getContext().getResources().getStringArray(getLockDetail().getStringArrayId())[getLockDetail().getArrayIndex()],
				getContext().getResources().getStringArray(getLockMaterial().getStringArrayId())[getLockMaterial().getArrayIndex()]
				);
	}

	public void setLockDetail(LockDetail lockDetail) {
		this.lockDetail = lockDetail;
	}

	public ArmorType getArmorType() {
		return armorType;
	}

	public void setArmorType(ArmorType armorType) {
		this.armorType = armorType;
	}

	public int getDurability() {
		return durability;
	}

	public void setDurability(int durability) {
		this.durability = durability;
	}

	public WeaponType getWeaponType() {
		return weaponType;
	}

	public void setWeaponType(WeaponType weaponType) {
		this.weaponType = weaponType;
	}
	
	public boolean isEquippable(){
		return getType()==ItemType.ARMOR || getType()==ItemType.WEAPON;
	}

}
